package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.excel.ExcelMeicineDistribution;
import com.caifu.mapper.SysUserMapper;
import com.caifu.mapper.YlBedRecordMapper;
import com.caifu.mapper.YlElderlyInfoMapper;
import com.caifu.mapper.YlMeicineDistributionMapper;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlElderlyInfo;
import com.caifu.pojo.YlMeicineDistribution;
import com.caifu.service.YlMeicineDistributionService;
import com.caifu.util.Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author tww
 * ClassName: YlMeicineDistributionServiceImpl.java、
 * createTime: 2020年11月12日 11:09:57
 **/
@Service("ylMeicineDistributionService")
public class YlMeicineDistributionServiceImpl implements YlMeicineDistributionService {

    @Resource
    private YlMeicineDistributionMapper ylMeicineDistributionMapper;

    @Resource
    private YlBedRecordMapper bedRecordMapper;

    @Resource
    private YlElderlyInfoMapper elderlyInfoMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public Result addMeicineDistribution(YlMeicineDistribution ylMeicineDistribution, SysUser user) {
        ylMeicineDistribution.setCreateTime(new Date());
        ylMeicineDistribution.setTellerNo(String.valueOf(user.getUserNo()));
        ylMeicineDistribution.setStatus("1");
        ylMeicineDistributionMapper.insert(ylMeicineDistribution);
        return ResultUtil.success(200, "success", "保存成功");
    }

    @Override
    public YlMeicineDistribution getMeicineDistributionById(String id) {
        YlMeicineDistribution ylMeicineDistribution = ylMeicineDistributionMapper.selectById(id);
        SysUser user = sysUserMapper.selectById(ylMeicineDistribution.getTellerNo());
        if (Util.isNotNull(user)) {
            ylMeicineDistribution.setTellerNo(user.getUserName());
        }
        return ylMeicineDistribution;
    }

    @Override
    public IPage<YlMeicineDistribution> getgetMeicineDistributionByIdByUserNo(String userNo, long currentPage, long pageSize) {
        IPage<YlMeicineDistribution> ylMeicineDistributionIPage = ylMeicineDistributionMapper.selectPage(new Page<>(currentPage, pageSize), new QueryWrapper<YlMeicineDistribution>()
                .eq("STATUS", 1)
                .eq("USER_NO", userNo)
                .orderByDesc("CREATE_TIME"));
        List<YlMeicineDistribution> records = ylMeicineDistributionIPage.getRecords();
        //获取当前时间
        long time = new Date().getTime();
        for (YlMeicineDistribution record : records) {
            //判断时间差
            if (time < record.getStartDate().getTime()) {
                record.setFlag("未开始");
            } else if (time > record.getStartDate().getTime() && time < record.getEndDate().getTime()) {
                record.setFlag("正常");
            } else if (time > record.getEndDate().getTime()) {
                record.setFlag("已结束");
            }
        }
        return ylMeicineDistributionIPage;

    }

    @Override
    public void deleteById(String id) {
        YlMeicineDistribution ylMeicineDistribution = ylMeicineDistributionMapper.selectById(id);
        ylMeicineDistribution.setStatus("9");
        ylMeicineDistributionMapper.updateById(ylMeicineDistribution);
    }


    /**
     * 计算时间差
     */
    public static long getDatePoor(Date nowDate, Date date) {
        long nd = 1000 * 24 * 60 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = nowDate.getTime() - date.getTime();
        // 计算差多少天
        long day = diff / nd;
        return day;
    }


    @Override
    public List<List<String>> getExcelHead() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<>();
        head0.add("档案编号");

        List<String> head1 = new ArrayList<>();
        head1.add("姓名");

        List<String> head2 = new ArrayList<>();
        head2.add("性别");

        List<String> head3 = new ArrayList<>();
        head3.add("年龄");

        List<String> head4 = new ArrayList<>();
        head4.add("补贴每餐（元）");

        List<String> head5 = new ArrayList<>();
        head5.add("餐费（元）");

        List<String> head6 = new ArrayList<>();
        head6.add("送餐费（元）");

        List<String> head7 = new ArrayList<>();
        head7.add("每客单价（元）");


        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        list.add(head6);
        list.add(head7);


        //送餐天数
        for (int i = 1; i < 29; i++) {
            List<String> head8 = new ArrayList<>();
            head8.add("送餐(天)");
            head8.add(String.valueOf(i));
            list.add(head8);
        }

        List<String> head9 = new ArrayList<>();
        head9.add("合计（天）");

        List<String> head10 = new ArrayList<>();
        head10.add("补贴金额（元）");

        List<String> head11 = new ArrayList<>();
        head11.add("餐费（元）");

        List<String> head12 = new ArrayList<>();
        head12.add("送餐费（元）");

        List<String> head13 = new ArrayList<>();
        head13.add("实收金额（元）");

        List<String> head14 = new ArrayList<>();
        head14.add("送餐人");

        List<String> head15 = new ArrayList<>();
        head15.add("备注");

        list.add(head9);
        list.add(head10);
        list.add(head11);
        list.add(head12);
        list.add(head13);
        list.add(head14);
        list.add(head15);


        return list;


    }

    @Override
    public List<ExcelMeicineDistribution> getExcelData(String userNo) {
        return ylMeicineDistributionMapper.getExcelData(userNo);
    }


    @Override
    public Map<String, Object> getUserInfo(String userNo) {
        Map<String, Object> map = new HashMap<>(10);
        //查询老人信息
        YlElderlyInfo ylElderlyInfo = elderlyInfoMapper.selectById(userNo);
        if (Util.isNotNull(ylElderlyInfo)) {
            map.put("userName", ylElderlyInfo.getUserName());
            String man = "1";
            String woman = "2";
            String sex = "";
            if (man.equals(ylElderlyInfo.getUserSex())) {
                sex = "男";
            } else if (woman.equals(ylElderlyInfo.getUserSex())) {
                sex = "女";
            } else {
                sex = "未知";
            }
            map.put("userSex", sex);
        }

        Map<String, Object> bedInfo = bedRecordMapper.queryOldUserBedInfo(userNo);
        if (Util.isNotNull(bedInfo)) {
            map.put("buildingName", bedInfo.get("BUILDING_NAME").toString());
            map.put("roomName", bedInfo.get("ROOM_NAME").toString());
            map.put("bedName", bedInfo.get("BED_NAME").toString());
        }
        return map;
    }
}
